<?php echo $this->render('link/header.phtml') ?>
//常用变量初始化
var menuDelUrl=baseUrl+"/link/serverclient/type/del";
var menuEditUrl=baseUrl+"/link/serverclient/type/edit";
var module="client";
var gridTitle="客户列表";
var dataId="client_id";
var Client = Ext.data.Record.create([
           {name: 'client_id', type: 'int'},
           {name: 'client_company', type: 'string'},
		   {name: 'client_industry', type: 'string'},
           {name: 'client_host', type: 'string'},
           {name: 'client_keyword', type: 'string'},
           {name: 'client_keyword_link', type: 'string'},
           {name: 'client_keyword_count', type: 'float'},
      ]);

industry_combo_store.load();      
var industry_combo=new Ext.form.ComboBox({
										 fieldLabel:'role',
										 store:industry_combo_store,										
        								displayField:'industry_name',
        								valueField :'industry_id',
										hiddenName:"industry_id",
        								typeAhead:true,
        								mode:'local',
        								triggerAction:'all',
                                        editable:false,  
        								emptyText:'请选择...',
        								selectOnFocus:true
										 });    
var ds=new Ext.data.Store({
							  proxy:new Ext.data.HttpProxy({
									url:baseUrl+'/link/serverclient/type/getAll'
									}),
							  reader:new Ext.data.JsonReader({
									root:'results',
									totalProperty:'total',
									id:'u_id'
									},                                    
                                    Client)
	});	

//定义列模型
var cm=new Ext.grid.ColumnModel([	
							new Ext.grid.CheckboxSelectionModel(),
                            {header:'客户ID',dataIndex:'client_id',sortable: true,width:30,hidden:true,editor:new Ext.form.TextField({allowBlank:false})},														
                            {header:'域名',
                            dataIndex:'client_host',
                            width:100,editor:new Ext.form.TextField({allowBlank:false})},
							{header:'公司名称',dataIndex:'client_company',width:150,editor:new Ext.form.TextField({allowBlank:false})},
							{header:'所属行业',dataIndex:'client_industry',width:150,editor:industry_combo,renderer:function(value,cell,record){                           
                            var index=industry_combo.getStore().find(industry_combo.valueField,value);
                            var record=industry_combo.getStore().getAt(index);                         
                           	if(record){
                            return record.data.industry_name;
                            }
                            else{
                            return value;
                            }
                            }},							
                            {header:'关键词',dataIndex:'client_keyword',width:200,hidden:true},						
                            {header:'关键词链接',dataIndex:'client_keyword_link',width:200,hidden:true},						
                            {header:'关键词次数',dataIndex:'client_keyword_count',width:100,hidden:true}						
	]);
cm.defaultSortable = true; 



//定义工具栏								 
var tb=new Ext.Toolbar({
							items:[
							<?php if($this->acl->isAllowed($this->role,'link','clientDel')) {?>
                                         {
                                            id:'menuDel'+ExtId,
											text:'删除',
											iconCls:'icn-delete',
											handler:menuDel
											},
                           <? } ?>
                           <?php if($this->acl->isAllowed($this->role,'link','clientEdit')) {?>                 
                                            {
											id:'menu_save'+ExtId,
											text:'保存',	
											iconCls:'icn-edit',
											disabled:true,
											handler:menuSave
											},{
											xtype:'tbfill'
											},
                            <? } ?>
                           <?php if($this->acl->isAllowed($this->role,'link','clientAdd')) {?>                
                                            {
                                            id:'menu_quickadd'+ExtId,
											text:'快速添加',
											iconCls:'icn-add',
											handler:menuAddQuick
										}
                            <? } ?>
											 
								 ]
	});			
   
function  menuAdd(){

}		
function menuAddQuick(){
	
	var n = grid.getStore().getCount();// 获得总行数
                var p = new Client({
                    client_company:"",
                    client_industry: '',
					client_host:'www.'
                });
                grid.stopEditing();// 停止编辑
                ds.insert(0, p);// 插入到第一行
                grid.startEditing(n, 1);// 开始编辑1单元格
                Ext.fly(grid.getView().getRow(0)).slideIn('t');
                Ext.fly(grid.getView().getRow(0)).frame('#cadaf9',2);
				Ext.getCmp('menu_save'+ExtId).setDisabled(false);
	}			      
<?php $gridId=uniqid(); ?>
//每页显示记录数
var mypagedata=[
				['10','10'],
				 ['15','15'],
				 ['20','20'],
				 ['40','40'],
				 ['200','200']
				 ];	
var page_size_store=new Ext.data.SimpleStore({
									   fields:[
											   {name:'id'},
											   {name:'name'},
									   		  ]
									   });
page_size_store.loadData(mypagedata);
var pagesize_combo=new Ext.form.ComboBox({
										 store:page_size_store,
										 width:50,										
										 emptyText:'10',
										 mode:'local',
										 triggerAction:'all',
										 valueField:'name',
										 displayField:'id'
										 });
pagesize_combo.on('select',function(e){										
										var pagesize=parseInt(e.getValue());
										pagBar.pageSize=pagesize;
										ds.load({params:{start:0,limit:pagesize}});
										});	

//定义分页条栏				 
var pagBar=new Ext.PagingToolbar({
										pageSize:30,
										store:ds,
										displayInfo:true,
										displayMsg:'第{0}到{1}条记录 共{2}条记录',
										emptyMsg:'没有数据记录',
										//plugins: filters,
										items:['-','每页显示',pagesize_combo,'条记录','-']
									 });                          
//定义容器
var grid=new Ext.grid.EditorGridPanel({
									renderTo:'grid_<?=$gridId?>',
									store:ds,
									cm:cm,									
									height:500,
									minHeight:300,
									autoWidht:true,								
									loadMask:{msg:'加载服务端数据中',store:ds},	
									autoScroll:true,
									stripeRows:true,									
        							animCollapse: false,
									monitorWindowResize: true,	
									title:gridTitle,
									clicksToEdit:true,                                   
									tbar:tb,
                                    sm:new Ext.grid.CheckboxSelectionModel({singleSelect:false}),
									bbar:pagBar,                                    
                                    stateful:true,  
									stateId:'link_'+module
									});
//显示数据
ds.load({params:{start:0,limit:30}});
grid.render();

grid.on("afteredit",afterEdit,grid);

function afterEdit(e){
	Ext.getCmp('menu_save'+ExtId).setDisabled(false);	
	}

function menuDel(){												
												var selectRows=grid.getSelectionModel().getSelections();												
												if(selectRows.length==0){
													Ext.MessageBox.alert('提示','请选择一条数据');
													}
												else{		
													var message="";
													for(var i=0;i<selectRows.length;i++){
														if(i!=0){
															message+=",";
															}
														message+=selectRows[i].get(dataId);
														}
													Ext.Ajax.request({
																	 url:menuDelUrl,
																	 params:{id:message},																																		 																	success:function(response,option){
																var temp=Ext.util.JSON.decode(response.responseText);																
																ds.load({params:{start:0,limit:30}});
																grid.render();
																		 },
																	failure:function(response,option){
																		var temp=Ext.util.JSON.decode(response.responseText);
																		Ext.MessageBox.alert('waring',temp.msg);
																		}
																	 });
													}
												}

function menuSave(){
	afterEditSave();
	Ext.getCmp("menu_save"+ExtId).setDisabled(true);
}

function afterEditSave(){
	var　modified　=　ds.modified;
	var json=[];
	Ext.each(modified,function(field){
						json.push(field.data);
						});
	if(json.length>0){
		Ext.Ajax.request({
						 url		: menuEditUrl,
						 params		: {data:Ext.util.JSON.encode(json)},
						 method		: 'POST',
						 success	: function(response){
							 	Ext.Msg.alert('信息','数据更新成功',function(){																	
										Ext.getCmp('content').getActiveTab().getUpdater().refresh();																
								});
							 }
						 });
		}
	else{
		Ext.Msg.alert("信息","没有修改任何信息");
		}
	//alert(e.field);
	//alert(e.record.get('role'));
}
 
});
</script>
<body style="width:98%;height:100%">
<div id='grid_<?=$gridId?>'></div>
</body>
</html>

